<template>
  <div class="myform">
    <div class="regist">
      <el-card>
        <!-- 提示区域 -->
        <el-alert
          title="填写上架货物信息"
          type="info"
          center
          show-icon
          :closable="false"
        ></el-alert>
        <!-- 步骤条 -->
        <div class="steps-box">
          <el-steps
            :space="200"
            :active="activeIndex - 0"
            finish-status="success"
            align-center
            :disabled="false"
          >
            <el-step title="个人信息"></el-step>
            <el-step title="商品详情"></el-step>
            <el-step title="完成"></el-step>
          </el-steps>
        </div>

        <!-- tab区 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="80px"
          label-position="top"
        >
          <el-tabs
            :tab-position="'left'"
            v-model="activeIndex"
            :before-leave="beforeTabLeave"
          >
            <el-tab-pane label="个人信息" name="0">
              <el-form-item label="个人姓名" prop="seller">
                <el-input
                  type="text"
                  v-model="addForm.seller"
                  disabled
                ></el-input>
              </el-form-item>
              <el-form-item label="联系电话" prop="seller_phone">
                <el-input
                  type="tel"
                  v-model="addForm.seller_phone"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item label="所属学校" prop="school">
                  <el-select size="small" v-model="addForm.school" clearable placeholder="请选择学校">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
              </el-form-item>
            </el-tab-pane>
            <el-tab-pane label="商品详情" name="1">
              <el-form-item label="商品名称" prop="goods_name">
                <el-input
                  type="text"
                  v-model="addForm.goods_name"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item label="所属类型" prop="goods_type">
                  <el-select size="small" v-model="addForm.goods_type" clearable placeholder="请选择类型">
    <el-option
      v-for="item in options1"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
              </el-form-item>
              <el-form-item label="现定价格" prop="goods_newprice">
                <el-input
                  type="number"
                  v-model="addForm.goods_newprice"
                  maxlength="8"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item label="原定价格" prop="goods_oldprice">
                <el-input
                  maxlength="8"
                  type="number"
                  v-model="addForm.goods_oldprice"
                  clearable
                ></el-input>
              </el-form-item>
               <el-form-item label="商品描述" prop="goods_describe">
                <el-input
                  type="textarea"
                  v-model="addForm.goods_describe"
                  clearable
                ></el-input>
              </el-form-item>
            </el-tab-pane>
            <el-tab-pane label="上传商品图片" name="2">
              <!-- action 要上传到的api地址 -->
              <el-alert
                title="商品正面、侧面、背面、商品简介图等 至少3张"
                type="warning"
                center
                show-icon
                :closable="false"
              ></el-alert>
              <el-upload
                multiple
                list-type="picture"
                ref="upload"
                name="img"
                :action="uploadURL"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
              >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>

             
            </el-tab-pane>
          </el-tabs>
        </el-form>
        <el-button v-show="activeIndex < 2" @click="next" id="nextBtn">下一步</el-button>
      </el-card>
    </div>
        <div class="show-regist" v-if="addForm.seller_phone">
            <p >个人姓名：{{addForm.seller}}</p>
            <p v-show="addForm.seller_phone">联系方式：{{addForm.seller_phone}}</p>
            <p v-show="addForm.school">所属学校：{{addForm.school}}</p>
            <p v-show="addForm.goods_name">商品名称：{{addForm.goods_name}}</p>
            <p v-show="addForm.goods_type">所属类型：{{addForm.goods_type}}</p>
            <p v-show="addForm.goods_newprice">现定价格：{{addForm.goods_newprice}}</p>
            <p v-show="addForm.goods_oldprice">原定价格：{{addForm.goods_oldprice}}</p>
            <p v-show="addForm.goods_describe">商品描述：{{addForm.goods_describe}}</p>
           
             <el-button
                size="small"
                type="success"
                class="successBtn"
                @click="AddAllForm"
                >申请上架</el-button
              >
              <el-button type="info" @click="resetRegist">重置</el-button>
        </div>
    <el-dialog title="图片预览" :visible.sync="isshowPhoto" width="50%">
      <img :src="previewPath" alt="" width="100%" class="sphoto" />
    </el-dialog>
  </div>
</template>

<script>
import { setGoodsHandle } from "network/request.js";
export default {
  data() {
    return {
      previewPath: "",
      activeIndex: 0,
      isshowPhoto: false,
      //    商品分类列表
      cateList: [],
       options: [{
          value: '重庆人文科技学院',
          label: '重庆人文科技学院'
        }, {
          value: '重庆大学',
          label: '重庆大学'
        },{
          value: '重庆师范大学',
          label: '重庆师范大学'
        }],
         options1: [{
          value: '电子设备',
          label: '电子设备'
        }, 
         {
          value: '学习图书',
          label: '学习图书'
        },{
          value: '服饰',
          label: '服饰'
        },{
          value: '其他',
          label: '其他'
        }],
      addForm: {
        seller:window.sessionStorage.getItem('uname'),
        seller_phone:"",
        school: "",
        goods_name: "",
        goods_type:"",
        goods_newprice:"",
        goods_oldprice:"",
        goods_describe:"",
        files: [],
      
      },
      // 上传的url地址
      uploadURL: "http://localhost:3001/upload",
      addFormRules: {
        seller_phone: [
          {
            required: true,
            pattern: /^1[34578]\d{9}$/,
            message: "请输入正确的联系电话",
            trigger: "blur",
          },
        ],
        school: [
          {
            required: true,
            message: "请选择所属学校",
            trigger: "change",
          },
        ],
         goods_name: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur",
          },
        ],
         goods_type: [
          {
            required: true,
            message: "请选择商品类型",
            trigger: "change",
          },
        ],
         goods_newprice: [
          {
            required: true,
            message: "请输入现价",
            trigger: "blur",
          },
        ],
        goods_oldprice: [
          {
            required: true,
            message: "请输入原价",
            trigger: "blur",
          },
        ],
         goods_describe: [
          {
            required: true,
            message: "请输入商品描述",
            trigger: "blur",
          },
        ],
       
       
      },
    };
  },
  methods: {
    resetRegist() {
      this.$refs.addFormRef.resetFields();
    },
    goLogin() {
      this.$router.push("/");
    },
    
    next() {
      if (
        this.activeIndex == 0 &&
        this.addForm.seller.length != 0 &&
        this.addForm.seller_phone.length != 0 &&
        this.addForm.school.length != 0
      ) {
        this.activeIndex = String(parseInt(this.activeIndex) + 1);
       
      } else if (
        this.addForm.goods_name.length != 0 &&
        this.addForm.goods_type.length != 0 &&
        this.addForm.goods_newprice.length != 0 &&
        this.addForm.goods_oldprice.length != 0 &&
        this.addForm.goods_describe.length != 0
      ) {
        this.activeIndex = String(parseInt(this.activeIndex) + 1);
        
      }
    },
    // 切换tabs触发
    beforeTabLeave(activeName, oldActiveName) {
      if (oldActiveName == 0 && this.addForm.school.length == 0) {
        this.$message.error("请输入正确的用户信息！");

        return false;
      } else {
        switch (Number(activeName)) {
          case 0:
            this.activeIndex = "0";
            break;
          case 1:
            this.activeIndex = "1";
            console.log(this.activeIndex);

            break;
          case 2:
            this.activeIndex = "2";
            console.log(this.activeIndex);

            break;
        }
      }
    },

    // 处理图片预览效果
    handlePreview(file) {
      this.previewPath = file.response.path;
      console.log(file);

      this.isshowPhoto = true;
    },
    // 处理图片移除时操作
    handleRemove(file) {
      // 获取将要删除的图片临时路径
      const filePath = file.response.path;
      // 拿到索引值
      const i = this.addForm.files.findIndex((x) => {
        x.pic === filePath;
      });
      // 移除
      this.addForm.files.splice(i, 1);
    },
    // 监听图片上传成功事件
    handleSuccess(response) {
      // 先拼接得到一个图片信息对象
      console.log(response);
      const picInfo = response.path;
      // 将图片信息对象，push到pics数组中
      this.addForm.files.push(picInfo);
    },
    // 新建实验
    AddAllForm() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error("请填写必要的商品信息！");
        }  else if (this.addForm.files.length == 0) {
          return this.$message.error("请必须上传商品图片！");
        } else {
          // 执行添加业务逻辑
          const { data: res } = await setGoodsHandle(this.addForm);
          this.$refs.addFormRef.resetFields();
          return this.$message.success("恭喜你上传商品成功！");
          this.$router.push('/')
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.myform {
  padding: 100px 0px;
  height: 100%;
  display: flex;
  width: 80%;
  margin: 0 auto;
 .regist {
     flex: 2;
     height: 100%;
     .el-card {
         min-height: 500px;
     }
 }
 .show-regist {
     margin-left: 50px;
     flex: 1;
     p {
         padding: 20px 0px;
     }
 }
  
  // border: 1px solid red;
  position: relative;
    .el-select {
        width: 100%;
    }
  .el-steps {
    justify-content: center;
  }
  #nextBtn {
    background-color: skyblue;
    margin: 2% 45%;
    color: white;
  }
  .el-tab-pane {
    width: 100%;
    .el-form-item {
      width: 40% !important;
      padding: 0px 20%;
    }
  }
}
#downTime {
  text-decoration: none;
  font-weight: bold;
  color: skyblue;
  font-size: 16px;
}
.successBtn {
  margin: 3% auto;
}
</style>